<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <el-form-item prop="equipName">
              <el-input v-model="filters.equipName" clearable filterable placeholder="摄像头名称"></el-input>
            </el-form-item>
            <el-form-item prop="equipCode">
              <el-input v-model="filters.equipCode" clearable filterable placeholder="摄像头编号"></el-input>
            </el-form-item>
            <el-form-item prop="cameraType">
              <el-select v-model="filters.cameraType" clearable filterable placeholder="摄像头类型">
                <el-option v-for="item in cameraTypeList" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="equipMac">
              <el-input v-model="filters.equipMac" filterable clearable placeholder="MAC地址"></el-input>
            </el-form-item>
            <el-form-item prop="equipState">
              <el-select v-model="filters.equipState" clearable filterable placeholder="在线状态">
                <el-option v-for="item in equipStateList" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item prop="beeperCode">
              <el-input v-model="filters.beeperCode" filterable clearable placeholder="呼叫器编号"></el-input>
            </el-form-item>
            <!-- <el-form-item prop="equipMac">
                <el-input v-model="filters.equipMac" clearable placeholder="MAC地址"></el-input>
              </el-form-item> -->
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
            <el-button type="danger" plain icon="fa fa-unlink" v-has="permissionAdd" @click="handleUnbinding" size="mini">批量解绑</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table
        :data="dataList"
        ref="tableList"
        :fit="true"
        highlight-current-row
        element-loading-text="数据加载中，请稍后..."
        empty-text="对不起，没有找到相关数据"
        @row-click="$refs.tableList.toggleRowSelection($event)"
        @selection-change="handleSelectChange"
      >
        <el-table-column type="selection" align="center"></el-table-column>
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>

        <el-table-column prop="equipName" label="摄像头名称" width="150" align="center"></el-table-column>
        <el-table-column prop="equipNickName" label="摄像头昵称" width="150" align="center"></el-table-column>
        <el-table-column prop="equipCode" label="摄像头编号" width="150" align="center"></el-table-column>
        <el-table-column prop="equipMac" label="MAC地址" width="170" align="center"></el-table-column>
        <el-table-column prop="cameraType" label="摄像头类型" :formatter="formatCameraType" align="center" width="150"> </el-table-column>
        <el-table-column label="呼叫器编号" align="center" width="150">
          <template slot-scope="scope">
            <span>{{ scope.row.beeperCode }}</span>
          </template>
        </el-table-column>
        <el-table-column label="在线状态" lign="center" width="100" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatEquipStates(scope.row.equipState)">
                {{ formatEquipState(scope.row.equipState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="报警状态" width="100" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.errorState==0" style="color:green">正常</span>
              <span v-if="scope.row.errorState==1" style="color:red">报警</span>
            </template>
          </el-table-column> -->
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{ scope.row.state === 0 ? '启用' : '禁用' }}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" @click.native="handleClickDelete(scope.row)" v-if="scope.row.state === 0">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionHeartbeat" @click.native="handleHeartbeatRecord(scope.row)">心跳记录</el-dropdown-item>
                <el-dropdown-item v-has="permissionHeartbeatMonitor" @click.native="handleViewVideoRecord(scope.row)">查看监控录像</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <HeartbeatTable ref="heartbeatTable" />
    <AlarmLogTable ref="alarmLogTable" />
  </div>
</template>

<script>
import * as message from '@/constants/message';
import HeartbeatTable from '@/components/HeartbeatTable';
import AlarmLogTable from '@/components/AlarmLogTable';
import CameraApi from '@/api/equip/camera';
import SafetyApi from '@/api/equip/safety';
export default {
  name: 'CameraManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      filters: {
        beeperCode: '',
        cameraType: '',
        equipName: '',
        equipCode: '',
        cameraType: '',
        equipMac: '',
        equipState: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      selectedList: [],
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_SEE',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_DELETE',
      permissionUpStatus: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_STATE',
      permissionHeartbeat: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_HEARTBEAT',
      permissionHeartbeatMonitor: 'SYSTEM_MANAGER_EQUIP_CAMERA_LIST_MONITOR',
      cameraTypeList: [
        //设备类型列表
        { id: 1, name: '红外' },
      ],
      equipStateList: [
        { id: 0, name: '未开通' },
        { id: 1, name: '未激活' },
        { id: 2, name: '在线' },
        { id: 3, name: '离线' },
      ],
    };
  },
  components: {
    HeartbeatTable,
    AlarmLogTable,
  },
  mounted() {
    this.init();
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    this.listData();
  },
  destroyed() {
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    // 通过id获取呼叫器名称
    getBeeperNameById(id) {
      return this.beeperList.find((item) => item.id == id);
    },
    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    formatCameraType(row, column, cellValue) {
      if (cellValue === 1) {
        return '红外';
      }
    },
    formatState(cellValue) {
      if (cellValue === -1) {
        return '删除';
      } else if (cellValue === 0) {
        return '禁用';
      } else if (cellValue === 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue === -1) {
        return 'danger';
      } else if (cellValue === 0) {
        return 'info';
      } else if (cellValue === 1) {
        return 'success';
      }
    },
    formatEquipState(cellValue) {
      if (cellValue === 0) {
        return '未开通';
      } else if (cellValue === 1) {
        return '未激活';
      } else if (cellValue === 2) {
        return '在线';
      } else if (cellValue == 3) {
        return '离线';
      }
    },
    formatEquipStates(cellValue) {
      if (cellValue === 0) {
        return 'danger';
      } else if (cellValue === 1) {
        return '';
      } else if (cellValue === 2) {
        return 'success';
      } else if (cellValue === 3) {
        return 'info';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        equipName: _this.filters.equipName,
        equipCode: _this.filters.equipCode,
        cameraType: _this.filters.cameraType,
        equipMac: _this.filters.equipMac,
        beeperCode: _this.filters.beeperCode,
        equipState: _this.filters.equipState,
      };
      // 发送请求
      CameraApi.listCamera(cond)
        .then((res) => {
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
        })
        .catch((err) => {});
    },
    // 调用心跳记录
    handleHeartbeatRecord(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/cameraDetail',
        query: { id: row.cameraId, recordType: 'heartBeat' },
      });
    },

    // 调用报警记录列表
    hanldeAlarmLogRecord(val) {
      this.$refs.alarmLogTable.handleCreate(val.equipId);
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state === 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        CameraApi.deleteCamera({
          id: row.cameraId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该摄像头', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.cameraId,
          state: -1,
        };
        CameraApi.deleteCamera(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/cameraDetail',
        query: { id: row.cameraId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/equip/cameraEdit',
        query: { id: val.cameraId },
      });
    },
    handleViewVideoRecord(val) {
      let _this = this;
      _this.$router.push({
        path: '/equip/cameraLiveBroadcast',
        query: { equipName: val.equipName, equipMac: val.equipMac, safetyCode: val.safetyCode },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/equip/cameraAdd');
    },
    handleSelectChange(val) {
      this.selectedList = val;
    },
    handleUnbinding() {
      if (this.selectedList.length == 0) {
        this.$message.warning('请至少选择一项');
        return;
      }

      let arr = this.selectedList.filter((item) => item.beeperCode);
      if (arr.length == 0) {
        this.$message.warning('请选择已经绑定呼叫器的设备');
        return;
      }
      let params = {
        safetyEquipIds: arr.map((item) => item.equipId),
      };
      this.$confirm('确定解除绑定吗?', '提示', { params }).then((res) => {
        SafetyApi.safetyBatchUnBinding(params).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
  },
};
</script>

<style lang="scss">
@import '@/assets/css/views/content.scss';
</style>
